<!--_meta 作为公共模版分离出去-->
<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="Bookmark" href="/favicon.ico" >
    <link rel="Shortcut Icon" href="/favicon.ico" />
    <!--[if lt IE 9]>
    <script type="text/javascript" src="../js/html5shiv.js"></script>
    <script type="text/javascript" src="../js/respond.min.js"></script>
    <![endif]-->
    <link href="../css/H-ui.min.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="../css/H-ui.css" />
    <link rel="stylesheet" type="text/css" href="../css/Hui-iconfont/1.0.8/iconfont.css" />
    <link rel="stylesheet" type="text/css" href="../css/skin/default/skin.css" id="skin" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <title></title>
</head>
<body>
<article class="page-container userUpdate">
    <form action="" method="post" class="form form-horizontal" id="form-user-add">
        <input type="hidden" id="userIdChild" user-id-child= ""/>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>用户姓名：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="name" name="name">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>登录账号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="account" name="account">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>手机号：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <input type="text" class="input-text" value="" placeholder="" id="mobile" name="mobile">
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>超级管理员：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <select id="superAdmin">
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>复核人：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <select id="checkManager">
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>可用：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <select id="enabled">
                    <option value="0">否</option>
                    <option value="1">是</option>
                </select>
            </div>
        </div>

        <div class="row cl">
            <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span>角色：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <div class="selectbox">

                    <div class="select-bar">
                        <select multiple="multiple" id="select1">
                            <!--<option value="超级管理员">超级管理员</option>
                            <option value="普通管理员">普通管理员</option>
                            -->
                        </select>
                    </div>
                    <div class="btn-bar">
                        <span id="add"><input type="button" class="btn" value=">"/></span><br />
                        <span id="add_all"><input type="button" class="btn" value=">>"/></span><br />
                        <span id="remove"><input type="button" class="btn" value="<"/></span><br />
                        <span id="remove_all"><input type="button" class="btn" value="<<"/></span>
                    </div>

                    <div class="select-bar">
                        <select multiple="multiple" id="roles" ></select>
                        <div class="errorMsg">角色信息不能为空！</div>
                    </div>

                </div>
            </div>
        </div>


        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <button class="btn btn-success radius submit" name=""><i class="icon-ok"></i> 确定</button>
            </div>
        </div>
    </form>
</article>

<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="/js/layer/2.4/layer.js"></script>
<script type="text/javascript" src="/js/H-ui.js"></script>
<script type="text/javascript" src="/js/H-ui.min.js"></script>
<script type="text/javascript" src="/js/postJson.js"></script>
<script type="text/javascript" src="/js/root.js"></script>
<script type="text/javascript" src="/js/jquery.validation/jquery.validate.js"></script>
<script type="text/javascript" src="/js/jquery.validation/validate-methods.js"></script>
<script type="text/javascript" src="/js/jquery.validation/messages_zh.js"></script>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->

<script type="text/javascript">
    var pageNum = parent.$("#pageNum").attr("data-pageNum")//当前页码
/*页面初始化*/
function updataEdie(){
    //获取父窗口roleId
    var userId =  Number(parent.$("#userId").attr("data-userid"))
    $("#userIdChild").attr("user-id-child",userId)
    var dataCollect="id="+Number(userId);
    dataCollect= decodeURIComponent(dataCollect,true);//防止中文乱码
    var jsondata=strJSON.formToJson(dataCollect);//转化为json
    var url = "/sysUser/queryOneByUserId";
    $.ajax({
        url:url,
        contentType:"application/json",
        data:jsondata,
        type:"post",
        async:false,
        dataType:'json',
        success:function(msg){
            // console.log(JSON.stringify(msg))
            $("#name").val(msg.data.user[0].name)
            $("#account").val(msg.data.user[0].account)
            $("#mobile").val(msg.data.user[0].mobile)
            $("#enabled option").eq(msg.data.user[0].enabled).attr("selected",true)
            $("#superAdmin option").eq(msg.data.user[0].superAdmin).attr("selected",true)
            $("#checkManager option").eq(msg.data.user[0].checkManager).attr("selected",true)

            var htmlString = '';
            var htmlString3 = '';
            $.each(msg.data.flag,function(i,n){
                htmlString += '<option value="'+ n.name+'" roleId="'+ n.id+'">'+ n.name+'</option>'
            })
            $("#select1").append(htmlString)


            var arrRole = [];
            if($("#superAdmin option").eq(0)){
                for(var key in msg.data.roleIds){
                    arrRole.push(msg.data.roleIds[key].roleId)
                }
                for(var i=0;i<msg.data.flag.length;i++){
                    for(var j=0;j<arrRole.length;j++){
                        if(msg.data.flag[i].id == arrRole[j]){
                            $("#select1").find("option[roleid='"+arrRole[j]+"']").remove();
                            htmlString3 += "<option data-add='"+i+"' roleid='"+arrRole[j]+"'>"+msg.data.flag[i].name+"</option>"
                        }
                    }
                }
                $("#roles").append(htmlString3)
            };


        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
            if(XmlStatus==401){
                alert(XmlError.error401)
                parent.window.location.href = "../login.html"
            }
            if(XmlStatus==403){
                alert(XmlError.error403)
            }
        }
    });
}
updataEdie()


    /*验证*/
    $("#form-user-add").validate({
        rules:{
            name:{
                required:true
            },
            account:{
                required:true
            },
            mobile:{
                required:true,
                isMobile:true
            },
            password:{
                required:true
            },
            checkManager:{
                required:true
            }

        },
        submitHandler:function(form){
            if($("#superAdmin").find("option:selected").val()!=1){
            if($("#roles option").length==0){
                $(".select-bar .errorMsg").show()
                var msgShow = setTimeout(function(){
                    $(".select-bar .errorMsg").hide()
                },2000);
                msgShow;
                return false
                }
            }
            $(form).ajaxSubmit(function(){
                $(".select-bar .errorMsg").hide();

                var name = $("#name").val()
                var account = $("#account").val()
                var mobile = $("#mobile").val()
                var password = $("#password").val()
                var superAdmin = Number($("#superAdmin option:checked").val())
                var enabled = Number($("#enabled option:checked").val())
                var checkManager=Number($("#checkManager option:checked").val())
                var userIdChild = $("#userIdChild").attr("user-id-child")

                var arr= {
                    "id":userIdChild,
                    "name":name,
                    "account":account,
                    "password":password,
                    "mobile":mobile,
                    "enabled":enabled,
                    "superAdmin":superAdmin,
                    "checkManager":checkManager,
                    "roles":[]
                }
                var url = "/sysUser/updateUser";

                $("#roles option").each(function(){
                    arr.roles.push(Number($(this).attr("roleid")))
                })
                var arrPost = JSON.stringify(arr)
                // console.log(arrPost)

                $.ajax({
                    url:url,
                    contentType:"application/json",
                    data:arrPost,
                    type:"post",
                    async:false,
                    dataType:'json',
                    success:function(msg){
//                        console.log(JSON.stringify(msg))
                        if(msg.success) {
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.msg("修改成功！",{icon:1,time:3000});
                            window.parent.frames.getData(Number(pageNum));
                            parent.layer.close(index);
                        }else {
                            var error = msg.errorMessage;
                            parent.layer.msg(error,{icon:2,time:3000});
                        }
                    },
                    error:function(XMLHttpRequest,textStatus,errorThrown){
                        var XmlStatus = XMLHttpRequest.status
//                         console.log(XMLHttpRequest.status)
                        if(XmlStatus==401){
                            alert(XmlError.error401)
                            parent.window.location.href = "../login.html"
                        }
                        if(XmlStatus==403){
                            alert(XmlError.error403)
                        }
                    }
                });


            });
        }
    });




    /*用户管理-用户信息-密码重置*/
    function user_updata_pass(title,url,w,h){
        layer_show(title,url,w,h);
    }
    /*管理员-角色-修改*/
    function user_update_edit(title,url,id,w,h){
        layer_show(title,url,w,h);
    }

</script>

<script>
    $(function(){
        $("#superAdmin").bind("change",function(){

            if($(this).find("option:selected").val()==1){
                if($("#roles option").length!=0){
                    var optClone='';
                    $("#roles").each(function(){

                        optClone = $(this).find("option").clone();
                        $("#roles option").remove()
                    })
                    $("#select1").append(optClone);
                    $('#add,#remove,#add_all,#remove_all,#select1,#roles').unbind()
                }
            }else{
                //移到右边
//            $('#add').click(function(){
                $('#add').bind("click",function(){
                    //获取选中的选项，删除并追加给对方
                    $('#select1 option:selected').appendTo('#roles');
                });

                //移到左边
                $('#remove').bind("click",function(){
                    $('#roles option:selected').appendTo('#select1');
                });

                //全部移到右边
                $('#add_all').bind("click",function(){
                    //获取全部的选项,删除并追加给对方
                    $('#select1 option').appendTo('#roles');
                });

                //全部移到左边
                $('#remove_all').bind("click",function(){
                    $('#roles option').appendTo('#select1');
                });

                //双击选项
                $('#select1').bind("dblclick",function(){ //绑定双击事件
                    //获取全部的选项,删除并追加给对方
                    $("option:selected",this).appendTo('#roles'); //追加给对方
                });

                //双击选项
                $('#roles').bind("dblclick",function(){
                    $("option:selected",this).appendTo('#select1');
                });
            }
        });


        if( $("#superAdmin").find("option:selected").val()==1){
            var optClone='';
            $("#roles").each(function(){

                optClone = $(this).find("option").clone();
                $("#roles option").remove()
            })
            $("#select1").append(optClone);
            $('#add,#remove,#add_all,#remove_all,#select1,#roles').unbind()
        }else{
            //移到右边
//            $('#add').click(function(){
            $('#add').bind("click",function(){
                //获取选中的选项，删除并追加给对方
                $('#select1 option:selected').appendTo('#roles');
            });

            //移到左边
            $('#remove').bind("click",function(){
                $('#roles option:selected').appendTo('#select1');
            });

            //全部移到右边
            $('#add_all').bind("click",function(){
                //获取全部的选项,删除并追加给对方
                $('#select1 option').appendTo('#roles');
            });

            //全部移到左边
            $('#remove_all').bind("click",function(){
                $('#roles option').appendTo('#select1');
            });

            //双击选项
            $('#select1').bind("dblclick",function(){ //绑定双击事件
                //获取全部的选项,删除并追加给对方
                $("option:selected",this).appendTo('#roles'); //追加给对方
            });

            //双击选项
            $('#roles').bind("dblclick",function(){
                $("option:selected",this).appendTo('#select1');
            });
        }
    });
    function ifTrue(data){
        if(data==1){
            return "是"
        }else{
            return "否"
        }
    }
</script>


<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>